/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gerenciadorfinanceiro.ejb.session;

import org.gerenciadorfinanceiro.ejb.session.interfaces.IncomeSessionLocal;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.gerenciadorfinanceiro.ejb.entity.Income;
import org.gerenciadorfinanceiro.ejb.entity.User;
import org.gerenciadorfinanceiro.ejb.session.interfaces.IncomeSessionRemote;

/**
 *
 * @author eduardo
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class IncomeSessionBean implements IncomeSessionLocal, IncomeSessionRemote {

    @PersistenceContext
    private EntityManager entityManager;
    
    public List<Income> getIncomes(User u, int month, int year) {
        Query q = entityManager.createQuery("SELECT i FROM Income i WHERE owner = :owner and month(data) = :month and year(data) = :year");
        q.setParameter("owner", u);
        q.setParameter("month", month);
        q.setParameter("year", year);

        return q.getResultList();
    }

    public Income getIncomeById(int id) {
        return entityManager.find(Income.class, id);
    }

    public void addIncome(Income i) throws BusinessViolation {
        this.validaIncome(i);
        
        entityManager.merge(i);
    }

    public void updateIncome(Income i) throws BusinessViolation {
        this.validaIncome(i);
        
        if (this.getIncomeById(i.getId().intValue()) == null) {
            throw new BusinessViolation("Receita inexistente.");
        }

        entityManager.merge(i);
    }

    public void deleteIncome(Income i) throws BusinessViolation {
        if (this.getIncomeById(i.getId().intValue()) == null) {
            throw new BusinessViolation("Receita inexistente.");
        }

        Income aux = entityManager.getReference(Income.class, i.getId());
        entityManager.remove(aux);
    }

    public void validaIncome(Income i) throws BusinessViolation {
        boolean valid = true;
        String msg = "";

        if (i.getOwner() == null) {
            valid = false;
            msg = "Onwer invalido.";
        }

        if (i.getDescriptionIncome() == null || i.getDescriptionIncome().isEmpty()) {
            valid = false;
            msg = "Descricao invalida.";
        }

        if (i.getData() == null) {
            valid = false;
            msg = "Data invalida.";
        }

        if (i.getValue() <= 0) {
            valid = false;
            msg = "Valor invalido.";
        }

        if (! valid) {
            throw  new BusinessViolation(msg);
        }
    }
 
}
